/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui.dialog;

import com.googlecode.jaden.common.other.Utils;
import com.googlecode.jaden.ui.Constants;
import com.googlecode.jaden.ui.UiUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Method;

public class AboutDialog extends JWindow {

    public AboutDialog(Frame owner) {
        super(owner);

        JLabel imageLabel = new JLabel(UiUtils.getImageIcon("images/neuron_about.png"));

        final JLabel linkLabel = new JLabel("<html><a href=\""
                + Constants.DIRECT_LINK + "\">" + Constants.DIRECT_LINK_TEXT + "</a></html>");
        linkLabel.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
            }

            @SuppressWarnings({"CallToRuntimeExec"})
            public void mouseReleased(MouseEvent e) {
                String osName = System.getProperty("os.name");
                try {
                    if (osName.startsWith("Mac OS")) {
                        Class fileMgr = Class.forName("com.apple.eio.FileManager");
                        Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[]{String.class});
                        openURL.invoke(null, Constants.DIRECT_LINK);
                    } else if (osName.startsWith("Windows")) {
                        Runtime.getRuntime().exec(
                                "rundll32 url.dll,FileProtocolHandler " + Constants.DIRECT_LINK);
                    } else { // Assume Unix or Linux
                        String[] browsers = {"firefox", "opera", "konqueror",
                                "epiphany", "mozilla", "netscape"};
                        String browser = null;
                        for (int count = 0; count < browsers.length && browser == null; count++) {
                            if (Runtime.getRuntime().exec(
                                    new String[]{"which", browsers[count]}).waitFor() == 0) {
                                browser = browsers[count];
                            }
                        }
                        if (browser == null) {
                            throw new Exception("Could not find web browser");
                        }
                        Runtime.getRuntime().exec(new String[]{browser, Constants.DIRECT_LINK});
                    }
                } catch (Exception ex) {
                    Utils.handleException(ex);
                }
            }

            public void mouseEntered(MouseEvent e) {
                UiUtils.setHandCursor(linkLabel);
            }

            public void mouseExited(MouseEvent e) {
                UiUtils.clearCursor(linkLabel);
            }
        });
        linkLabel.setForeground(Color.BLUE);

        JLabel titleLabel = new JLabel(Utils.translate(Constants.LABEL_UI_TITLE));
        titleLabel.setFont(new Font(titleLabel.getFont().getName(), Font.BOLD, titleLabel.getFont().getSize()));

        JLabel versionLabel = new JLabel(Constants.DIRECT_VERSION + " (" + Constants.DIRECT_MONIKER + ')');
        versionLabel.setFont(new Font(versionLabel.getFont().getName(), Font.BOLD, versionLabel.getFont().getSize()));
        JPanel p2 = UiUtils.horizontalList(titleLabel, versionLabel);
        p2.setOpaque(true);
        p2.setBackground(Color.WHITE);

        JPanel p3 = UiUtils.horizontalList(new JLabel(Utils.translate(Constants.LABEL_BUILT_ON_TEXT)),
                new JLabel(Constants.DIRECT_BUILD_DATE));
        p3.setOpaque(true);
        p3.setBackground(Color.WHITE);

        JPanel p4 = UiUtils.horizontalList(linkLabel);
        p4.setOpaque(true);
        p4.setBackground(Color.WHITE);

        JLabel copyrightLabel = new JLabel("    " + Utils.translate(Constants.LABEL_COPYRIGHT_TEXT) +
                Constants.DIRECT_COPYRIGHT);
        copyrightLabel.setForeground(Color.GRAY);
        JPanel pc = UiUtils.horizontalList(copyrightLabel);
        pc.setBackground(Color.WHITE);
        pc.setOpaque(true);

        JPanel center = UiUtils.verticalList(p2, p3, p4);

        JLabel west = new JLabel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(50, (int) super.getPreferredSize().getHeight());
            }
        };
        west.setBackground(Color.WHITE);
        west.setOpaque(true);

        JPanel outer = new JPanel(new BorderLayout());
        add(outer, BorderLayout.CENTER);
        outer.setBorder(BorderFactory.createLineBorder(Color.gray));

        outer.add(imageLabel, BorderLayout.NORTH);
        outer.add(center, BorderLayout.CENTER);
        outer.add(west, BorderLayout.WEST);
        outer.add(pc, BorderLayout.SOUTH);

        pack();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);

        addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
                dispose();
            }

            public void mousePressed(MouseEvent e) {
                dispose();
            }

            public void mouseReleased(MouseEvent e) {
                dispose();
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });
    }
}
